package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.spzx.common.redis.service.RedisService;
import com.spzx.product.domain.Product;
import com.spzx.product.domain.ProductDetails;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.domain.SkuStock;
import com.spzx.product.mapper.ProductDetailsMapper;
import com.spzx.product.mapper.ProductMapper;
import com.spzx.product.mapper.ProductSkuMapper;
import com.spzx.product.mapper.SkuStockMapper;
import com.spzx.product.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 商品 服务实现类
 * </p>
 *
 * @author biu
 * @since 2025-03-13
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private SkuStockMapper skuStockMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Product> selectProductList() {

        return baseMapper.selectProductList();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertProduct(Product product) {

        //product
        product.setStatus(0); //初始状态
        baseMapper.insert(product);

        //product_sku
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (int i = 0; i < productSkuList.size(); i++) {
            ProductSku productSku = productSkuList.get(i);
            productSku.setSkuCode(product.getId() + "_" + i);
            productSku.setSkuName(product.getName() + " " + productSku.getSkuSpec());
            productSku.setProductId(product.getId());
            productSku.setStatus(0); //初始状态

            productSkuMapper.insert(productSku);


            //sku_stock
            SkuStock skuStock = new SkuStock();
            skuStock.setSkuId(productSku.getId());
            skuStock.setTotalNum(productSku.getStockNum());
            skuStock.setAvailableNum(productSku.getStockNum());
            skuStock.setLockNum(0);
            skuStock.setSaleNum(0);
            skuStockMapper.insert(skuStock);
        }

        //product_details
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(
                String.join(",", product.getDetailsImageUrlList())
        );

        productDetailsMapper.insert(productDetails);

        return 1;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateStatus(Long id, Integer status) {
       //Product
        Product product = new Product();
        product.setId(id);
        product.setStatus(status);
        baseMapper.updateById(product);


        //productSku
        LambdaUpdateWrapper<ProductSku> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ProductSku::getProductId,id);
        updateWrapper.set(ProductSku::getStatus,status);

        productSkuMapper.update(null, updateWrapper);


        //获取当前商品下的所有sku
        LambdaQueryWrapper<ProductSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductSku::getProductId, id).select(ProductSku::getId);
        List<Object> productSkuIdList = productSkuMapper.selectObjs(queryWrapper);

        //更新bitmap
        if(status == 1){
            productSkuIdList.forEach(skuId -> {
                redisTemplate.opsForValue().setBit("product:sku:data:ids", (Long)skuId, true);
            });
        }else if(status == -1){
            productSkuIdList.forEach(skuId -> {
                redisTemplate.opsForValue().setBit("product:sku:data:ids", (Long)skuId, false);
            });
        }


        return 1;
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteProductByIds(List<Long> ids) {

        //product
        baseMapper.deleteBatchIds(ids);


        //查询所有的skuIdList

        //product_sku
        LambdaQueryWrapper<ProductSku> queryWrapperSku = new LambdaQueryWrapper<>();
        queryWrapperSku.in(ProductSku::getProductId, ids).select(ProductSku::getId);
        List<Object> productSkuIdList = productSkuMapper.selectObjs(queryWrapperSku);

        productSkuMapper.delete(queryWrapperSku);

        //product_details
        LambdaQueryWrapper<ProductDetails> queryWrapperDetails = new LambdaQueryWrapper<>();
        queryWrapperDetails.in(ProductDetails::getProductId, ids);
        productDetailsMapper.delete(queryWrapperDetails);

        //sku_stock
        LambdaQueryWrapper<SkuStock> queryWrapperStock = new LambdaQueryWrapper<>();
        queryWrapperStock.in(SkuStock::getSkuId, productSkuIdList);
        skuStockMapper.delete(queryWrapperStock);

        return 1;

    }


}
